package com.cn.redis2.dao;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
 * 
 * @author Han
 */
public class BaseRedisDao<K, V>{
	
	@Autowired
	private RedisTemplate<K, V> redisTemplate;
	
	@Resource(name="redisTemplate")
	protected ValueOperations<K, V> valueOperations;
	//
	@Resource(name="redisTemplate")
	protected HashOperations<K, Object, Object> hashOperations;
	//
	@Resource(name="redisTemplate")
	protected ListOperations<K, V> listOperations;
	
	
	/**
	 * 
	 * @param key
	 * @param value
	 * @param expire
	 * @return
	 */
	protected void set(K key, V value, long expire) {
		valueOperations.set(key, value, expire, TimeUnit.SECONDS);
	}

	/**
	 * get value
	 * 
	 * @param key
	 * @return
	 */
	protected V get(K key) {
		return valueOperations.get(key);
	}
	
	/**
	 * key delete
	 * @param key
	 */
	protected void delete(K key){
		getRedisTemplate().delete(key);
	}
	
	/**
	 * key exist
	 * @param key
	 * @return
	 */
	protected boolean hasKey(K key){
		return  getRedisTemplate().hasKey(key);
	}
	/**
	 *key expire 
	 * @param key
	 * @param timeout
	 * @param unit
	 * @return
	 */
	protected Boolean expire(K key,long timeout,TimeUnit unit){
		return getRedisTemplate().expire(key, timeout, unit);
	}
	/**
	 * redistemplate是全局唯一的，子类不要出现对redistemplate的成员变量的设置(比如keyserializer,)
	 * @return
	 */
	RedisTemplate<K, V> getRedisTemplate() {
		return redisTemplate;
	}
	/**
	 * 当需要更改serializer,可以直接通过connection.set等方法实现
	 * @param callback
	 * @return
	 */
	protected <T> T execute(RedisCallback<T> callback){
		return redisTemplate.execute(callback);
	}
	/**
	 * 获取stringserializer
	 */
	protected RedisSerializer<String> getStringSerializer(){
		return redisTemplate.getStringSerializer();
	}
	/**
	 * 获取JdkSerializationRedisSerializer
	 */
	protected <T> RedisSerializer<T> getDefaultSerializer(){
		return (RedisSerializer<T>) redisTemplate.getDefaultSerializer();
	}
	
	protected  RedisSerializer<K> getKeySerializer(){
		return (RedisSerializer<K>) redisTemplate.getKeySerializer();
	}
	
	protected <T> RedisSerializer<T> getHashKeySerializer() {
		return (RedisSerializer<T>) redisTemplate.getHashKeySerializer();
	}
	
	protected <T> RedisSerializer<T> getHashValueSerializer() {
		return (RedisSerializer<T>) redisTemplate.getHashValueSerializer();
	}
}
